package com.rjhch.tonglink.client.jms.tutorial;

import javax.jms.*; // The JMS classes
import javax.naming.*; // The JNDI classes

class PtoP {
    public static final String tcf = "tongtech.jms.jndi.JmsContextFactory";/* initial context factory*/
    public static final String localURL = "tlqlocal://localhost/qcu1";
    public static final String remoteURL = "tlkq://127.0.0.1:10025";
    public static final String localFactory = "LocalConnectionFactory";
    public static final String remoteFactory = "RemoteConnectionFactory";

    
    final static String url = "tlqlocal://localhost/qcu1";

    public static void main(String args[]) {
    String url = null;
    String factory = null;
        java.util.Properties prop = new java.util.Properties();
		//Read command line arguments
		for (int i = 0; i < args.length; i++) {
			if (args[i].startsWith("-")) {
				if (args[i].equals("-protocol")) {
					String protocol = args[++i];
					if (protocol.equals("local")) {
						url = localURL;
						factory = localFactory;
					} else {
						url = remoteURL;
						factory = remoteFactory;
					}
				} else if ( (args[i].equals("-h")) || (args[i].equals("-?")) ) {
					usage("HELP: ");
					return;
				}  else {
					usage("Unknown option: " + args[i++]);
					return;
				}
			} else {
				//done with options
				usage("Extra arguments detected. first is: " + args[i]);
				return;
			}
		}
		        prop.put("java.naming.factory.initial", tcf);
        prop.put("java.naming.provider.url", url);
        /*初始化上下文*/
        Context ictx = null;
        Queue queue = null;
        QueueConnectionFactory qcf = null;
        try {
            ictx = new InitialContext(prop);
        } catch (NamingException ne) {
            System.out.println("error:" + ne);
            System.exit(-1);
        }
        try {
            // Look up connection factory and queue.
            qcf = (QueueConnectionFactory) ictx.lookup(factory);
            queue = (Queue) ictx.lookup("MyQueue");
        } catch (NamingException ne) {
            System.out.println("naming error:" + ne);
            System.exit(-1);
        }
        try{
            //Establish QueueConnection and QueueSession
            QueueConnection conn = qcf.createQueueConnection();
            
            QueueSession sess = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            /*send message*/
            QueueSender queueSender = sess.createSender(queue);
            Message message = sess.createTextMessage("A message body");
            queueSender.send(message);
            /*receive message*/
            QueueReceiver receiver = sess.createReceiver(queue);
            conn.start();
            Message     messagerecv = receiver.receive();
            
            if (messagerecv instanceof TextMessage) {
                String text = ((TextMessage) messagerecv).getText();
                System.out.println(text);
            } else {
                System.out.println("message is not TextMessage");
            }
            // 关闭使用完毕的对象
            queueSender.close();
            sess.close();
            conn.close();

        } catch (JMSException je) {
            System.out.println("jms error:" + je);
            System.exit(-1);
        }
    }

	public static void usage(String error) {
		
		if (error != null) {
			System.out.println(error);
		}
		System.out.println("Usage: PtoP -protocol <local|remote>");
		System.out.println("\tprotocol: protocol type, local or remote");
	}
}
